package com.anyikang.volunteer.sos.volunteer.app.main;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.anyikang.volunteer.sos.volunteer.app.bean.User;
import com.anyikang.volunteer.sos.volunteer.app.mine.MyRescueActivity;
import com.anyikang.volunteer.sos.volunteer.app.mine.SettingsActivity;
import com.anyikang.volunteer.sos.volunteer.app.userInfo.UserInfoActivity;
import com.anyikang.volunteer.sos.volunteer.app.view.PullZoomScollView;

import com.anyikang.volunteer.sos.volunteer.R;

import static com.nostra13.universalimageloader.core.ImageLoader.TAG;

public class MineFragment extends Fragment implements View.OnClickListener{

	private View layout;
	PullZoomScollView mPullZoomScrollView;
	private ImageView mImageView;  //顶部图片
	private ImageView avatarImg;  //头像
	private Activity mActivity;
	static final int REQUEST_USER_INFO = 0;
	private Fragment selfFragment;
	String TAG = "MineFragment";
	private RelativeLayout reLayoutMyRescue;
	private RelativeLayout reLayoutSettings;

	//菜单
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i(TAG,"onCreateView");

		selfFragment = this;
		layout = inflater.inflate(R.layout.fragment_me, container, false);
		initView();
		return layout;
	}

	@Override
	public void onAttach(Context context) {
		Log.i(TAG,"onAttach");
		mActivity = (Activity) context;
		super.onAttach(context);
	}


	@Override
	public void onHiddenChanged(boolean hidden) {
		Log.i(TAG,"onHiddenChanged="+hidden);
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onStart() {
		Log.i(TAG,"onStart");
		super.onStart();
	}

	@Override
	public void onResume() {

		Log.i(TAG,"onResume");
		initData();
		super.onResume();
	}

	@Override
	public void onStop() {
		Log.i(TAG,"onStop");
		super.onStop();
	}


	@Override
	public void onDestroyView() {
		Log.i(TAG,"onDestroyView");
		super.onDestroyView();
	}


	private void initView() {
		mPullZoomScrollView = (PullZoomScollView) layout.findViewById(R.id.pullZoomScrollview);
		mImageView = (ImageView) layout.findViewById(R.id.headerImv);
		avatarImg = (ImageView) layout.findViewById(R.id.avatarImg);
		mPullZoomScrollView.setZoomImageView(mImageView);


		reLayoutMyRescue = (RelativeLayout) layout.findViewById(R.id.relayoutMyRescue);
		reLayoutSettings = (RelativeLayout) layout.findViewById(R.id.reLayoutSettings);

		//事件
		avatarImg.setOnClickListener(this);
		reLayoutMyRescue.setOnClickListener(this);
		reLayoutSettings.setOnClickListener(this);
	}

	public void initData()
	{
		//读取用户信息，并赋值给各个控件
		User user = new User(mActivity);
		String headPicPath = user.getHeadPicPath();

		if(!TextUtils.isEmpty(headPicPath))
		{
			Bitmap bmp = BitmapFactory.decodeFile(headPicPath);
			avatarImg.setImageBitmap(bmp);
		}
		return;
	}



	@Override
	public void onClick(View view) {
		switch(view.getId())
		{
			case R.id.avatarImg:
			/*	Intent i = new Intent(mActivity, UserInfoActivity.class);
				selfFragment.startActivityForResult(i,REQUEST_USER_INFO);*/
				break;
			case R.id.relayoutMyRescue:  //我的救援
				Intent mySosIntent = new Intent(mActivity, MyRescueActivity.class);
				startActivity(mySosIntent);
				break;
			case R.id.reLayoutSettings:  //我的设置
				Intent settingIntent = new Intent(mActivity, SettingsActivity.class);
		        mActivity.startActivity(settingIntent);
				break;
		}

	}

}
